package com.lab.schemeurl;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;

import com.cuitrip.business.home.IndexActivity;
import com.cuitrip.push.PushService;
import com.lab.schemeurl.constant.Scheme;
import com.umeng.message.UTrack;
import com.umeng.message.entity.UMessage;

import org.json.JSONObject;


/**
 * Created by ak
 */
public class SchemeUrlActivity extends Activity {

    public static final String SchemeUrl = "SchemeUrl";

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();

        if (null != intent) {
            String scheme = intent.getScheme();
            Uri uri = intent.getData();

            if (!TextUtils.isEmpty(scheme) && null != uri && scheme.equals(Scheme.CUITRIP.getSchemeName())) {
                Class<?> targetClass;
                boolean isFromSchemeActivity = false;
                // Splash页面是否启动过,已启动,直接开启HomeActivity,否则先开启Splash再跳转至HomeActivity
                /*if (RuntimeGlobalInfo.booted) {
                    targetClass = HomeActivity.class;
                } else {
                    targetClass = SplashActivity.class;
                    isFromSchemeActivity = true;
                }*/
                if (intent.getBooleanExtra(PushService.PUSH_KEY, false)){
                    String messageBody = intent.getStringExtra(PushService.MESSAGE_BODY);
                    try {
                        JSONObject jsonObject = new JSONObject(messageBody);
                        UMessage msg = new UMessage(jsonObject);
                        UTrack.getInstance(this).trackMsgClick(msg);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                Intent homeIntent = new Intent(SchemeUrlActivity.this, IndexActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString(SchemeUrl, scheme);
                if (intent.hasExtra(PushService.USER_TYPE_KEY)) {
                    bundle.putInt(PushService.USER_TYPE_KEY, intent.getIntExtra(PushService.USER_TYPE_KEY, -1));
                }
                homeIntent.setData(uri);
                homeIntent.putExtras(bundle);
                startActivity(homeIntent);
            }
        }
        // 立即finish自身
        finish();
    }

}